<?php
namespace app\api\controller;
use think\Lang;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 第三方登录控制器
 */
class Connect extends MobileMall
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/login.lang.php');
    }


    /**
     * @api {POST} api/Connect/get_sms_captcha 短信动态码
     * @apiVersion 1.0.0
     * @apiGroup Connect
     *
     * @apiParam {String} phone 手机号
     * @apiParam {String} type 短信类型 1为注册,2为登录,3为找回密码
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Int} result.sms_time  发送倒计时
     */
    public function get_sms_captcha(){
        $state = lang('send_fail');
        $sms_mobile = input('param.phone');
        if (strlen($sms_mobile) == 11){
            $log_type = input('param.type');//短信类型:1为注册,2为登录,3为找回密码
                
            $state = 'true';
            $member_model = model('member');
            $member = $member_model->getMemberInfo(array('member_mobile' => $sms_mobile));
            $sms_captcha = rand(100000, 999999);
            //$sms_captcha = 123456;//测试数据
            //$log_msg = '【' . config('site_name') . '】您于' . date("Y-m-d");
            $log_msg = '您于' . date("Y-m-d");

            switch ($log_type) {
                case '1':
                    if (config('sms_register') != 1) {
                        $state = lang('system_obile_registration_function');
                    }
                    if (!empty($member) && $member['member_account_status'] == 1) {//检查手机号是否已被注册
                        $state = '当前手机号已被注册，请更换其他号码。';
                    }
                    $log_msg .= lang('register_member_dynamic_code').'：' . $sms_captcha . '。';
                    break;
                case '2':
                    /*if (config('sms_login') != 1) {
                        $state = lang('enable_mobile_phone_login');
                    }*/
                    if (empty($member)) {//检查手机号是否已绑定会员
                        $state = lang('check_correct_number');
                    }
                    $log_msg .= '申请登录，动态码：' . $sms_captcha . '。';
                    break;
                case '3':

                    if (config('sms_password') != 1) {
                        $state = lang('mobile_back_password');
                    }

                    if (empty($member)) {//检查手机号是否已绑定会员
                        $state = lang('check_correct_number');
                    }
                    
                    $log_msg .= '申请重置登录密码，动态码：' . $sms_captcha . '。';
                    break;
                default:
                    $state = lang('param_error');
                    break;
            }

            if ($state == 'true') {

                $smslog_model = model('smslog');
                $result = $smslog_model->sendSms($sms_mobile, $log_msg, $log_type, $sms_captcha, $member['member_id'], $member['member_name']);

                if ($result['state']) {
                    ds_json_encode(10000, lang('send_success'),array('sms_time' => 60));
                    exit;
                } else {
                    $state = $result['message'];
                }
            }
        }

        ds_json_encode(10001,$state);
    }
    /**
     * 验证注册动态码
     */
    public function check_sms_captcha(){
        $state = lang('validation_fails');
        $phone = input('param.phone');
        $captcha = input('param.captcha');
        $log_type=input('param.type');
        if (strlen($phone) == 11){
            $state = 'true';
            $condition = array();
            $condition['smslog_phone'] = $phone;
            $condition['smslog_captcha'] = $captcha;
            $condition['smslog_type'] = $log_type;
            $smslog_model = model('smslog');
            $sms_log = $smslog_model->getSmsInfo($condition);
            if(empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP-1800)) {//半小时内进行验证为有效
                $state = lang('dynamic_code_expired');
                ds_json_encode(10001,$state);
            }
            ds_json_encode(10000, '',$state);
        }
        ds_json_encode(10001,$state);

    }



    /**
     * @api {POST} api/Connect/sms_register 手机注册
     * @apiVersion 1.0.0
     * @apiGroup Connect
     *
     * @apiParam {String} phone 手机号
     * @apiParam {String} captcha 验证码
     * @apiParam {String} password 密码
     * @apiParam {Int} inviter_id 推荐人id
     * @apiParam {String} client 客户端类型
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Int} result.state  注册状态 1成功0失败
     * @apiSuccess {String} result.username  用户名称
     * @apiSuccess {String} result.key  用户token
     * @apiSuccess {Object} result.info 用户信息
     * @apiSuccess {Int} result.info.member_id  用户ID
     * @apiSuccess {Object} result.info.member_name  用户名称
     * @apiSuccess {Object} result.info.member_truename  真实姓名
     * @apiSuccess {Object} result.info.member_avatar  头像
     * @apiSuccess {Object} result.info.member_points  积分
     * @apiSuccess {Object} result.info.member_email  邮箱
     * @apiSuccess {Object} result.info.member_mobile  手机号
     * @apiSuccess {Object} result.info.member_qq  QQ
     * @apiSuccess {Object} result.info.member_ww  旺旺
     */
    public function sms_register(){
        $phone = input('post.phone');
        $captcha = input('post.captcha');
        $password = input('post.password');
        $client = input('post.client');
        $inviter_id = intval(input('post.inviter_id'));
        $logic_connect_api = model('connectapi','logic');
        $state_data = $logic_connect_api->smsRegister($phone, $captcha, $password, $client,$inviter_id);

        if($state_data['state']=='1'){
            $state_data['info'] = $this->getMemberUser($state_data['info']);
            ds_json_encode(10000, '',$state_data);
        } else {
            ds_json_encode(10001,$state_data['msg']);
        }
    }
    
    /**
     * 手机验证码登录
     */
    public function sms_login()
    {
        $member_mobile = input('post.usermobile');
        $mobilecode = input('post.mobilecode');
        
        if(empty($member_mobile) || empty($mobilecode)){
            ds_json_encode(10001,lang('param_error'));
        }
        if (config('sms_login') != 1) {
            ds_json_encode(10001,lang('enable_mobile_phone_login'));
        }
        $condition = array();
        $condition['smslog_phone'] = $member_mobile;
        $condition['smslog_captcha'] = $mobilecode;
        $condition['smslog_type'] = 2;
        $smslog_model = model('smslog');
        $sms_log = $smslog_model->getSmsInfo($condition);
        if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) {//半小时内进行验证为有效
            ds_json_encode(10001,lang('dynamic_code_expired'));
        }
        $member_model = model('member');
        $member = $member_model->getMemberInfo(array('member_mobile' => $member_mobile)); //获取当前手机号的用户信息
        if (!empty($member)) {
            if (!$member['member_state']) {//1为启用 0 为禁用
                ds_json_encode(10001, lang('login_index_account_stop'));
            }
            $token = $member_model->getBuyerToken($member['member_id'], $member['member_name'], input('post.client'));
            if($token) {
                ds_json_encode(10000, '',array('username' => $member['member_name'], 'token' => $token));
            }else{
                ds_json_encode(10001,lang('param_error'));
            }
        }else{
            ds_json_encode(10001,lang('mobile_not_exist'));
        }
        
    }



    /**
     * @api {POST} api/Connect/find_password 手机找回密码
     * @apiVersion 1.0.0
     * @apiGroup Connect
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} phone 手机号
     * @apiParam {String} captcha 验证码
     * @apiParam {String} password 密码
     * @apiParam {String} client 用户端 wap手机端
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Int} result.state  找回密码状态 1成功0失败
     * @apiSuccess {String} result.username  用户名称
     * @apiSuccess {String} result.key  用户token
     * @apiSuccess {Int} result.info.member_id  用户ID
     * @apiSuccess {String} result.info.member_name  用户名称
     * @apiSuccess {String} result.info.member_truename  真实姓名
     * @apiSuccess {String} result.info.member_avatar  头像
     * @apiSuccess {String} result.info.member_points  积分
     * @apiSuccess {String} result.info.member_email  邮箱
     * @apiSuccess {String} result.info.member_mobile  手机号
     * @apiSuccess {String} result.info.member_qq  QQ
     * @apiSuccess {String} result.info.member_ww  旺旺
     */
    public function find_password(){
        $phone = input('post.phone');
        $captcha = input('post.captcha');
        $password = input('post.password');
        $client = input('post.client');
        $logic_connect_api = model('connectapi','logic');
        $state_data = $logic_connect_api->smsPassword($phone, $captcha, $password, $client);
        
        if($state_data['state']){
            unset($state_data['state']);
            unset($state_data['msg']);
            $state_data['info']=$this->getMemberUser($state_data['info']);
            ds_json_encode(10000, '',$state_data);
        } else {            
            ds_json_encode(10001,$state_data['msg']);
        }
        
    }


    /**
     * 登录开关状态
     */
    public function get_state() {
        $logic_connect_api = model('connectapi','logic');
        $state_array = $logic_connect_api->getStateInfo();

        $key = input('param.t');
        if(trim($key) != '' && array_key_exists($key,$state_array)){
            ds_json_encode(10000, '',$state_array[$key]);
        } else {
            ds_json_encode(10001,lang('param_error'));
        }
    }


}